﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Backup;
using Microsoft.SharePoint; 

namespace SharePointManagementStudio
{
    public partial class frmRestoreBackup : Form
    {
        public frmRestoreBackup()
        {
            InitializeComponent();

            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);

        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                this.BeginInvoke(new MethodInvoker(delegate()
                {
                    
                        SPSite objSite = new SPSite(listBox1.SelectedItem.ToString());
                        objSite.AllowUnsafeUpdates = true;
                        SPWeb web = objSite.OpenWeb();
                        web.AllowUnsafeUpdates = true;

                        SPSiteCollection mySiteCols = objSite.WebApplication.Sites;

                        string backupfielname = label3.Text;
                        appendText("Starting Restor for " + listBox1.SelectedItem.ToString() + "\n");
                        mySiteCols.Restore(listBox1.SelectedItem.ToString(), @backupfielname, true);
                        appendText("Restore was complate.");
                        objSite.Close();
                    
                }));


            });
        }


        private void appendText(string p)
        {
            this.BeginInvoke(new MethodInvoker(delegate()
            {
                this.richTextBox1.Text += p;
            }));
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            toolStripProgressBar1.Value = 100;
            toolStripProgressBar1.Value = 0;
            timer1.Stop();
            this.button1.Enabled = true;               
        }
        private void frmRestoreBackup_Load(object sender, EventArgs e)
        {
            SPFarm spf = SPFarm.Local;
            SPWebService spws = spf.Services.GetValue<SPWebService>("");
            int sitecount = spws.WebApplications.Count;

            foreach (SPWebApplication spwa in spws.WebApplications)
            {
                try
                {
                    this.listBox1.Items.Add(spwa.Sites[0].RootWeb.Url.ToString());
                }
                catch
                {
                    //   AppendText("error accured when connect to :" + spwa.Name.ToString() + " site collection is not found or site correpted.\n"); 
                }
            }
        }

        private void toolStripButtonLoadFiles_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                panel1.Visible = true;
                label3.Text = openFileDialog1.FileName.ToString();                     
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count >0 )
            {
                button1.Enabled = false;
                timer1.Start();
                backgroundWorker1.RunWorkerAsync();
            }
            else
            {
                MessageBox.Show("Pleace select a web application");  
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (toolStripProgressBar1.Value == toolStripProgressBar1.Maximum)
            {
                toolStripProgressBar1.Value = 0;
            }
            toolStripProgressBar1.PerformStep();
        }
    }
}
